package org.jeecg.modules.jxc.sales_management.salelist_Search.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.api.R;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.jxc.sales_management.salelist_Search.entity.JxcSaleLisjxcGoods;
import org.jeecg.modules.jxc.sales_management.salelist_Search.entity.JxcSaleList;
import org.jeecg.modules.jxc.sales_management.salelist_Search.mapper.JxcSaleLisjxcGoodsMapper;
import org.jeecg.modules.jxc.sales_management.salelist_Search.service.IJxcSaleLisjxcGoodsService;
import org.jeecg.modules.jxc.sales_management.salelist_Search.service.IJxcSaleListService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: jxc_sale_list
 * @Author: jeecg-boot
 * @Date:   2021-12-20
 * @Version: V1.0
 */
@Api(tags="jxc_sale_list")
@RestController
@RequestMapping("/saleListGoods")
@Slf4j
public class JxcSaleListController extends JeecgController<JxcSaleList, IJxcSaleListService> {
	 @Autowired
	 private IJxcSaleListService jxcSaleListService;
	 @Autowired
	 private IJxcSaleLisjxcGoodsService jxcSaleLisjxcGoodsService;


	 //销售单查询
	 @AutoLog(value = "jxc_sale_list-分页列表查询")
	 @ApiOperation(value = "jxc_sale_list-分页列表查询", notes = "jxc_sale_list-分页列表查询")
	 @GetMapping(value = "/list")
	 public Result<?> queryPageList(String saleNumber,
	 		                        String saleDate_begin,
									String  saleDate_end,
									Integer customerId,
									Integer state,
									@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
									@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize
								) {




		 /*QueryWrapper<JxcSaleList> queryWrapper = QueryGenerator.initQueryWrapper(jxcSaleList, req.getParameterMap());
		 Page<JxcSaleList> page = new Page<JxcSaleList>(pageNo, pageSize);
		 IPage<JxcSaleList> pageList = jxcSaleListService.page(page, queryWrapper);*/

		 Page<JxcSaleList> page = new Page<>(pageNo, pageSize);
		 IPage<JxcSaleList> pageList = jxcSaleListService.getSalelistPage(page,saleDate_begin,saleDate_end,customerId,state,saleNumber);
		 return Result.ok(pageList);
	 }

	 /**
	  * 添加
	  *
	  * @param jxcSaleList
	  * @return
	  */
	 @AutoLog(value = "jxc_sale_list-添加2")
	 @ApiOperation(value = "jxc_sale_list-添加2", notes = "jxc_sale_list-添加2")
	 @PostMapping(value = "/add")
	 public Result<?> add(@RequestBody JxcSaleList jxcSaleList) {
		 System.out.println(jxcSaleList);
	 	jxcSaleListService.save(jxcSaleList);
		 return Result.ok("添加成功！");
	 }

	 /**
	  * 编辑
	  *
	  * @param jxcSaleList
	  * @return
	  */
	 @AutoLog(value = "jxc_sale_list-编辑")
	 @ApiOperation(value = "jxc_sale_list-编辑", notes = "jxc_sale_list-编辑")
	 @PutMapping(value = "/edit")
	 public Result<?> edit(@RequestBody JxcSaleList jxcSaleList) {
		 jxcSaleListService.updateById(jxcSaleList);
		 return Result.ok("编辑成功!");
	 }

	 /**
	  * 通过id删除
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "jxc_sale_list-通过id删除")
	 @ApiOperation(value = "jxc_sale_list-通过id删除", notes = "jxc_sale_list-通过id删除")
	 @DeleteMapping(value = "/delete")
	 public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
		 jxcSaleListService.removeById(id);
		 return Result.ok("删除成功!");
	 }

	 /**
	  * 批量删除
	  *
	  * @param ids
	  * @return
	  */
	 @AutoLog(value = "jxc_sale_list-批量删除")
	 @ApiOperation(value = "jxc_sale_list-批量删除", notes = "jxc_sale_list-批量删除")
	 @DeleteMapping(value = "/deleteBatch")
	 public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
		 this.jxcSaleListService.removeByIds(Arrays.asList(ids.split(",")));
		 return Result.ok("批量删除成功!");
	 }

	 /**
	  * 通过id查询
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "jxc_sale_list-通过id查询")
	 @ApiOperation(value = "jxc_sale_list-通过id查询", notes = "jxc_sale_list-通过id查询")
	 @GetMapping(value = "/queryById")
	 public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
		 JxcSaleList jxcSaleList = jxcSaleListService.getById(id);
		 if (jxcSaleList == null) {
			 return Result.error("未找到对应数据");
		 }
		 return Result.ok(jxcSaleList);
	 }

	 /**
	  * 导出excel
	  *
	  * @param request
	  * @param jxcSaleList
	  */
	 @RequestMapping(value = "/exportXls")
	 public ModelAndView exportXls(HttpServletRequest request, JxcSaleList jxcSaleList) {
		 return super.exportXls(request, jxcSaleList, JxcSaleList.class, "jxc_sale_list");
	 }

	 /**
	  * 通过excel导入数据
	  *
	  * @param request
	  * @param response
	  * @return
	  */
	 @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	 public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		 return super.importExcel(request, response, JxcSaleList.class);
	 }



	 //销售单保存
	 @SneakyThrows
	 @AutoLog(value = "jxc_sale_list-新增")
	 @ApiOperation(value = "jxc_sale_list-新增", notes = "jxc_sale_list-新增")
	 @PostMapping(value = "/save")
	 public Result<?> addSaleList(
			 @RequestParam("saleNumber") String saleNumber,
			 @RequestBody JxcSaleList jxcSaleList,
			 HttpServletRequest req
			/* @RequestParam("saleListGoodsStr") String saleListGoodsStr*/ ){


		 jxcSaleList.setSaleNumber(saleNumber);

		 jxcSaleListService.save(jxcSaleList);


		 String saleListGoodsStr = req.getParameter("saleListGoodsStr").replace("\'","");
		 ObjectMapper mapper = new ObjectMapper();
		 JxcSaleLisjxcGoods jxcGoods = mapper.readValue(saleListGoodsStr, JxcSaleLisjxcGoods.class);

		 System.out.println("================"+jxcGoods+"====================");
		 jxcSaleLisjxcGoodsService.save(jxcGoods);
		 return Result.ok("销售单保存成功！");

	 }



 }